﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Xml.Linq;

namespace PlayDeezGames.Common
{
    public static class PropertyFactories
    {
        private static Dictionary<string, string> typeToEntityFactoryTable = new Dictionary<string, string>();
        private static void SetEntityFactoryForType(string theType, string theFactory)
        {
            if (string.IsNullOrEmpty(theFactory))
            {
                if (typeToEntityFactoryTable.ContainsKey(theType))
                {
                    typeToEntityFactoryTable.Remove(theType);
                }
            }
            else
            {
                typeToEntityFactoryTable[theType] = theFactory;
            }
        }
        public static string GetEntityFactoryForType(string theType)
        {
            string result = Names.CommonFactories.PropertyFactory;
            if (typeToEntityFactoryTable.ContainsKey(theType))
            {
                result = typeToEntityFactoryTable[theType];
            }
            return result;
        }
        private static Dictionary<string, IPropertyFactory> factories = new Dictionary<string, IPropertyFactory>();
        public static void RegisterFactory(string theTypeName, IPropertyFactory theFactory,string theEntityFactoryName)
        {
            factories[theTypeName] = theFactory;
            SetEntityFactoryForType(theTypeName, theEntityFactoryName);
        }
        public static void UnregisterFactory(string theTypeName)
        {
            SetEntityFactoryForType(theTypeName, null);
            if (factories.ContainsKey(theTypeName))
            {
                factories.Remove(theTypeName);
            }
        }
        public static Dictionary<string, IPropertyFactory>.KeyCollection Types
        {
            get
            {
                return factories.Keys;
            }
        }
        public static object Load(string theTypeName, XElement theXElement)
        {
            if (factories.ContainsKey(theTypeName))
            {
                return factories[theTypeName].LoadFromXElement(theXElement);
            }
            return new IndexOutOfRangeException();
        }
        public static void Save(string theTypeName, XElement theXElement, object thePayload)
        {
            if (factories.ContainsKey(theTypeName))
            {
                factories[theTypeName].SaveToXElement(theXElement, thePayload);
                return;
            }
            throw new IndexOutOfRangeException();
        }
        public static object CreateNew(string theTypeName)
        {
            if (factories.ContainsKey(theTypeName))
            {
                return factories[theTypeName].CreateNew();
            }
            return new IndexOutOfRangeException();
        }
    }
}
